<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Tra ve url dan toi thu muc cua style hien tai
 * @param type $path
 * @return string
 */
function url_styles($path = '') {
	$CI = & get_instance();
	$style_path = $CI->config->item('style_path');
	$url_style = base_url() . $style_path . $path;
	return $url_style;
}

/**
 * Tạo url chung của site, để có thể sử dụng bởi redirect hoặc site_url
 * @param type $key
 * @param string $controller
 * @param string $method
 * @param string $var
 * @return type
 */
function url_create($key, $controller = '', $method = '', $var = '', $url = '') {
	$url = url_friendly($url);
	$CI = & get_instance();
	if ($controller != '')
		$controller = '/' . $controller;
	if ($method != '')
		$method = '/' . $method;
	if ($var != '')
		$var = '/' . $var;
	$id = preg_replace('/\/(\d+).*/', '$1', $var);
	$matches = array();
	preg_match('/\/\d+\/(\d+)/', $var, $matches);
	switch ($key) {
		case 'admin': {
				return base_url($CI->config->item('admin_folder') . $controller . $method . $var);
			}
		case 'site': {
				switch ($controller) {
					case '/ViewProduct':
						if ($method == '/index') {

							if ($url != '')
								return base_url('/album/' . $url . '-' . $id . '.html');
							return base_url('/album' . $var);
						}
					case '/ViewPage':
						if ($method == '/index') {

							if ($url != '')
								return base_url('/tin-tuc/' . $url . '-' . $id . '.html');
							return base_url('/tin-tuc' . $var);
						}
					case '/ViewCatalogue':
						if ($method == '/index') {
							if ($url != '')
								if (count($matches) > 0)
									return base_url('/catalog/' . $url . '-' . $id . '-trang-' . $matches[1] . '.html');
								else
									return base_url('/catalog/' . $url . '-' . $id . '.html');
							return base_url('/catalog' . $var);
						}
					default:
						return base_url($controller . $method . $var);
				}
			}
	}
}

function url_friendly($str) {
	$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
	$str = preg_replace("/(đ)/", 'd', $str);
	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
	$str = preg_replace("/(Đ)/", 'D', $str);
	$str = preg_replace("/(')/", '', $str);
	$str = preg_replace("/(\")/", '', $str);
	$str = preg_replace("/\,/", '', $str);
	$str = preg_replace("/\./", '', $str);
	$str = preg_replace("/\)/", '', $str);
	$str = preg_replace("/\(/", '', $str);
	$str = preg_replace("/\:/", '', $str);
	$str = preg_replace("/\!/", '', $str);
	$str = preg_replace("/\@/", 'at-', $str);
	$str = str_replace('-', '', $str);
	while(strpos($str, '  ')!==FALSE) $str = str_replace ('  ', ' ', $str);
	$str = trim($str);
	$str = str_replace(" ", "-", str_replace("&*#39;", "", $str));
	$str = strtolower($str);
	return $str;
}

?>
